package util;

import java.sql.*;

public class ProduceURL {

	/**
	 * 此类功能有关键词拼成公共搜索的可用链接，数据源是keywords表和produceURL.xml
	 * 注意：这个类没有调parameter类
	 * 
	 */
	public int getSearchURL() {
		Connection conn = null;
		Statement stmt = null;
		Statement stmtUpdate = null;
		ResultSet rs = null;
		UpdateParameter up = new UpdateParameter();
		//读xml	
		try {
			
			Class.forName("com.mysql.jdbc.Driver");
			/*
			 * 从keywords表的keyword字段拿到关键字，增加前后字符串后在写入wangzhi表的
			 * wangzhi字段
			 */
			conn = DriverManager.getConnection("jdbc:mysql://" + host + "/"
					+ dBName + "?user=root");			
			stmt = conn.createStatement();
			stmtUpdate = conn.createStatement();
			rs = stmt.executeQuery("select id,keyword,gbk from keywords");	
			while(rs.next())
			{
				String sql = "insert into wangzhi (wangzhi,kwd,sourceNet) values('" + first + rs.getString("gbk") + webSite + end + "','" + rs.getString("keyword") + "'," + sourceNet + ")";
				// System.out.println(sql);
				stmtUpdate.executeUpdate(sql);
				//每五次打印出来一次
				if(rs.getInt("id")%5==0)
				{
					up.setPUId(Integer.toString(rs.getInt("id")));
				}
				System.out.println("生成一组新搜索链接" + "-----" + dBName);
			}
			}

		 catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}  finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (stmtUpdate != null) {
					stmtUpdate.close();
					stmtUpdate = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 4;

	}
	//set方法传入参数
	String host = null;
	String dBName = null;
	String webSite = null;
	String first = null;
	String end = null;
	int sourceNet = 0;
	public void setSourceNet(int sourceNet) {
		this.sourceNet = sourceNet;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public void setdBName(String dBName) {
		this.dBName = dBName;
	}
	public void setWebSite(String webSite) {
		this.webSite = webSite;
	}
	public void setFirst(String first) {
		this.first = first;
	}
	public void setEnd(String end) {
		this.end = end;
	}

	
}
